package com.imcoke.abchat.web.controller;

import com.imcoke.abchat.common.AppProperties;
import com.imcoke.abchat.common.ResultEnum;
import com.imcoke.abchat.domain.json.JsonUrl;
import com.imcoke.abchat.domain.ResultBody;
import com.imcoke.abchat.common.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @Author ccoke
 * @Description
 * @Date: 14:52 2017-11-01
 */
@RestController
@RequestMapping("/upload")
public class FileController {
    @Autowired
    private AppProperties appProperties;

    @RequestMapping(value = "/image",method = RequestMethod.POST)
    public ResponseEntity<ResultBody> uploadImage(MultipartFile file, HttpServletRequest request){

        //保存图片到本地
        try {
            //获取图片保存真实路径
            String realPath = appProperties.getFile_path() + appProperties.getImage_path()+ File.separator;
            String urlPath = "/" +  appProperties.getImage_path() + "/";
            String url = FileUtil.imageUpLoad(file,realPath,urlPath);
            if(!"".equals(url)){
                return ResponseEntity.ok(new ResultBody(ResultEnum.SUCCESS,new JsonUrl(url)));
            }
        } catch (IOException e) {

        }
        return ResponseEntity.ok(new ResultBody(ResultEnum.UPLOADIMG_ERROR,""));
    }
}
